package com.ruoyi.business.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.bizsys.domain.*;
import com.ruoyi.bizsys.mapper.ProductDeferredInfoMapper;
import com.ruoyi.bizsys.service.ILoanIouService;
import com.ruoyi.bizsys.service.ILoanOrderService;
import com.ruoyi.bizsys.service.IRealNameAuthService;
import com.ruoyi.bizsys.service.ISmsTaskService;
import com.ruoyi.bizsys.utils.BizConstants;
import com.ruoyi.bizsys.vo.MonnifyReservedAccount;
import com.ruoyi.business.mapper.BRepaymentMapper;
import com.ruoyi.business.service.ICustVaService;
import com.ruoyi.business.service.IMonnifyService;
import com.ruoyi.business.service.IRepaymentSmsService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.sms.domain.SmsSendData;
import com.ruoyi.sms.service.ISmsService;
import com.ruoyi.system.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@Service
public class RepaymnetSmsServiceImpl extends BaseService implements IRepaymentSmsService {

    @Autowired
    private ISmsTaskService smsTaskService;

    @Autowired
    private ILoanIouService loanIouService;

    @Autowired
    private IRealNameAuthService realNameAuthService;

    @Autowired
    BRepaymentMapper repaymentMapper;

    @Autowired
    ICustVaService custVaService;
    ISmsService iSmsService;

    @Autowired
    ILoanOrderService orderService;

    @Autowired
    ISysConfigService sysConfigService;

    @Autowired
    ProductDeferredInfoMapper deferredInfoMapper;

    @Autowired
    IMonnifyService monnifyService;

    @Autowired
    ILoanOrderService loanOrderService;

    @Autowired
    ISmsService smsService;

    @Async("threadPoolTaskExecutor")
    @Override
    public void batchWithhold(LoanIou loanIou, String bathNo) {
        log.info("代扣短信IouStatus:{}", loanIou.getIouStatus());

        loanIou = loanIouService.selectLoanIouById(loanIou.getId());
        RealNameAuth realNameAuth = new RealNameAuth();
        realNameAuth.setCustNo(loanIou.getCustNo());
        realNameAuth = realNameAuthService.selectRealNameAuthList(realNameAuth).get(0);

        String lastName = realNameAuth.getLastName();
        String noticeType = BizConstants.SMS_NOTICE_TYPE_NOTICE;
        String smsType = "";
        JSONObject smsJson = new JSONObject();
        smsJson.put("lastName", lastName);
        BigDecimal collectAmt = isSuccess(bathNo, loanIou.getCustNo());
        ProductInfo productInfo = orderService.selectLoanOrderProduct(loanIou.getCustNo());

        if (loanIou.getIouStatus() == LoanIou.LOAN_IOU_STATUS_SETTLE) {
            //结清
            LoanOrder loanOrder = new LoanOrder();
            loanOrder.setLoanSerialNo(loanIou.getLoanSerialNo());
            List<LoanOrder> loanOrders = loanOrderService.selectLoanOrderList(loanOrder);
            smsJson.put("contractNo", loanOrders.get(0).getContractNo());
            smsJson.put("collectAmt", collectAmt);
            smsType = BizConstants.SMS_TYPE_WITHHOLD_SETTLE;
        } else if (loanIou.getIouStatus() == LoanIou.LOAN_IOU_STATUS_NORMAL) {
            //查找monnify账号
            MonnifyReservedAccount monnifyReservedAccount = custVaService.getVaReservedAccount(loanIou.getCustNo());
            //int productInfo = iSmsService.sendRepaymentSms(loanIou, realNameAuth, LoanIou.LOAN_IOU_STATUS_WAIT);
            if (productInfo != null && loanIou.getIsDeferred() == 0) {
                //发送延期短信
                smsJson.put("credit_rating", sysConfigService.selectConfigKey(productInfo.getCreditRating()));
                smsJson.put("repaymentDate", loanIou.getRepayDate());
                smsType = BizConstants.SMS_TYPE_COLL_RPYMT_NTCE_DFR_T_0;
                noticeType = BizConstants.SMS_NOTICE_TYPE_REPAYMENT;
            } else {
                if (collectAmt.doubleValue() > 0) {
                    smsJson.put("collectAmt", collectAmt);
                    smsType = BizConstants.SMS_TYPE_WITHHOLD_WAIT_SUCCESS;
                } else {
                    smsType = BizConstants.SMS_TYPE_WITHHOLD_WAIT_FAILED;
                }
            }
            BigDecimal needTotal = loanIou.getLoanPrincipal().add(loanIou.getLoanInterest()).add(loanIou.getServiceFee()).add(loanIou.getLateFee());
            BigDecimal repaidTotal = loanIou.getRepaidPrincipal().add(loanIou.getRepaidInterest()).add(loanIou.getRepaidServiceFee()).add(loanIou.getRepaidLateFee());
            BigDecimal repaymentAmt = needTotal.subtract(repaidTotal);
            smsJson.put("RemainingAmt", repaymentAmt.setScale(2, BigDecimal.ROUND_UP));

            smsJson.put("VAaccountnumber", monnifyReservedAccount.getAccountNumber());
            smsJson.put("BankName", monnifyReservedAccount.getBankName());

        } else if (loanIou.getIouStatus() == LoanIou.LOAN_IOU_STATUS_OVERDUE) {
            //逾期
            MonnifyReservedAccount monnifyReservedAccount = custVaService.getVaReservedAccount(loanIou.getCustNo());
            //int productInfo = iSmsService.sendRepaymentSms(loanIou, realNameAuth, LoanIou.LOAN_IOU_STATUS_OVERDUE);
            BigDecimal principal = loanIou.getLoanPrincipal().subtract(loanIou.getRepaidPrincipal());
            List<ProductDeferredInfo> productDeferredInfoList = deferredInfoMapper.getProductDerferedsForShow(productInfo.getId(), principal, loanIou.getPeriodNum(), productInfo.getDeferredMaxDays());
            //满足权益，未使用，在保留期
            if (productInfo != null && loanIou.getIsDeferred() == 0 && productDeferredInfoList.size() > 0 && DateUtils.apartDay(loanIou.getRepayDate()) <= productDeferredInfoList.get(0).getNumDeferredRetainDay()) {
                smsJson.put("overdueDay", DateUtils.apartDay(loanIou.getRepayDate()));
                smsType = BizConstants.SMS_TYPE_COLL_RPYMT_NTCE_DFR_T_M1;
                noticeType = BizConstants.SMS_NOTICE_TYPE_REPAYMENT;
            } else {
                if (collectAmt.doubleValue() > 0) {
                    smsJson.put("collectAmt", collectAmt);
                    smsType = BizConstants.SMS_TYPE_WITHHOLD_OVERDUE_SUCCESS;
                } else {
                    smsType = BizConstants.SMS_TYPE_WITHHOLD_OVERDUE_FAILED;
                }
            }
            BigDecimal needTotal = loanIou.getLoanPrincipal().add(loanIou.getLoanInterest()).add(loanIou.getServiceFee()).add(loanIou.getLateFee());
            BigDecimal repaidTotal = loanIou.getRepaidPrincipal().add(loanIou.getRepaidInterest()).add(loanIou.getRepaidServiceFee()).add(loanIou.getRepaidLateFee());
            BigDecimal repaymentAmt = needTotal.subtract(repaidTotal);
            smsJson.put("RemainingAmt", repaymentAmt.setScale(2, BigDecimal.ROUND_UP));

            smsJson.put("VAaccountnumber", monnifyReservedAccount.getAccountNumber());
            smsJson.put("BankName", monnifyReservedAccount.getBankName());
        }
        if (!("".equals(smsType))) {
            SmsTask smsTask = new SmsTask();
            smsTask.setUserId(realNameAuth.getUserId());
            smsTask.setCustNo(loanIou.getCustNo());
            smsTask.setCountryCode("+234");
            smsTask.setPhone(realNameAuth.getPhoneNumber());
            smsTask.setNoticeType(noticeType);
            smsTask.setSmsParam(JSON.toJSONString(smsJson));
            smsTask.setSmsType(smsType);
            smsTaskService.insertSmsTask(smsTask);
        }
    }

    @Async("threadPoolTaskExecutor")
    @Override
    public void monnify(LoanIou loanIou, BigDecimal amount) {
        if (loanIou.getIouStatus() == LoanIou.LOAN_IOU_STATUS_NORMAL) {
            return;
        }

        RealNameAuth realNameAuth = new RealNameAuth();
        realNameAuth.setCustNo(loanIou.getCustNo());
        realNameAuth = realNameAuthService.selectRealNameAuthList(realNameAuth).get(0);

        AppUser appUser = new AppUser();
        appUser.setUserUuid(realNameAuth.getUserId());
        List<AppUser> appUsers = appUserService.selectAppUserList(appUser);

        String lastName = realNameAuth.getLastName();
        String smsType = "";
        String templateId = "";
        JSONObject smsJson = new JSONObject();
        smsJson.put("lastName", lastName);
        log.info("订单状态{}", loanIou.getIouStatus());
        int i = 0;
        if (loanIou.getIouStatus() == LoanIou.LOAN_IOU_STATUS_SETTLE) {
            //结清
            LoanOrder loanOrder = new LoanOrder();
            loanOrder.setLoanSerialNo(loanIou.getLoanSerialNo());
            List<LoanOrder> loanOrders = loanOrderService.selectLoanOrderList(loanOrder);
            //还款成功
            i = smsService.sendRepaymentPassSms(loanOrders.get(0).getCustNo(), appUsers.get(0));
            if (i == 0) {
                smsJson.put("contractNo", loanOrders.get(0).getContractNo());
                smsJson.put("repaymentAmt", amount);
                smsType = BizConstants.SMS_TYPE_REPAYMENT_FINISH;
                templateId = "200017";
            }
        }
//        else if(loanIou.getIouStatus()==LoanIou.LOAN_IOU_STATUS_WAIT){
//
//            //正常
//            smsType = BizConstants.SMS_TYPE_REPAYEMNT_OUTSTADING;
//
//            BigDecimal needTotal = loanIou.getLoanPrincipal().add(loanIou.getLoanInterest()).add(loanIou.getServiceFee()).add(loanIou.getLateFee());
//            BigDecimal repaidTotal = loanIou.getRepaidPrincipal().add(loanIou.getRepaidInterest()).add(loanIou.getRepaidServiceFee()).add(loanIou.getRepaidLateFee());
//            BigDecimal repaymentAmt = needTotal.subtract(repaidTotal);
//            smsJson.put("remainingAmt", repaymentAmt.setScale(2,BigDecimal.ROUND_UP));
//            smsJson.put("rapaymentAmt", amount);
//            smsJson.put("repaymentDate", DateUtils.formatDate(DateUtils.parseDate(loanIou.getRepayDate()),"dd-MM-yyyy"));
//            smsJson.put("repayStatus", "success");
//            //查找monnify账号
//            String token = iExternalApiService.monnifyAuthLogin();
//            ExtMonnifyReservedAccounts extMonnifyReservedAccounts = iBRepaymentService.checkAccountReference(loanIou.getCustNo(), token);
//
//            smsJson.put("VAaccountnumber", extMonnifyReservedAccounts.getAccountsAccountNumber());
//            smsJson.put("BankName", extMonnifyReservedAccounts.getAccountsBankName());
//        }
        else if (loanIou.getIouStatus() == LoanIou.LOAN_IOU_STATUS_OVERDUE) {
            //逾期
            smsType = BizConstants.SMS_TYPE_REPAYMENT_OVERDUE;
            templateId = "200016";

            BigDecimal needTotal = loanIou.getLoanPrincipal().add(loanIou.getLoanInterest()).add(loanIou.getServiceFee()).add(loanIou.getLateFee());
            BigDecimal repaidTotal = loanIou.getRepaidPrincipal().add(loanIou.getRepaidInterest()).add(loanIou.getRepaidServiceFee()).add(loanIou.getRepaidLateFee());
            BigDecimal repaymentAmt = needTotal.subtract(repaidTotal);
            smsJson.put("repayStatus", "success");
            smsJson.put("repaymentAmt", amount);
            smsJson.put("remainingAmt", repaymentAmt.setScale(2, BigDecimal.ROUND_UP));
            //查找monnify账号
            MonnifyReservedAccount monnifyReservedAccount = custVaService.getVaReservedAccount(loanIou.getCustNo());

            smsJson.put("VAaccountnumber", monnifyReservedAccount.getAccountNumber());
            smsJson.put("BankName", monnifyReservedAccount.getBankName());
        }

        if (i == 0) {
            SmsSendData smsData = SmsSendData.builder().noticeType(BizConstants.SMS_NOTICE_TYPE_NOTICE).smsType(smsType)
                    .countryCode("234").phoneNumber(appUsers.get(0).getPhone()).tmplateId(templateId).build();

            smsData.setSmsParam(smsJson);

            smsService.sendSms(smsData);
        }


//        SmsTask smsTask = new SmsTask();
//        smsTask.setUserId(realNameAuth.getUserId());
//        smsTask.setCustNo(loanIou.getCustNo());
//        smsTask.setCountryCode("+234");
//        smsTask.setPhone(realNameAuth.getPhoneNumber());
//        smsTask.setNoticeType(BizConstants.SMS_NOTICE_TYPE_NOTICE);
//        smsTask.setSmsParam(JSON.toJSONString(smsJson));
//        smsTask.setSmsType(smsType);
//        iSmsTaskService.insertSmsTask(smsTask);
    }

    /**
     * 延期还款冲账后发送短信，根据老的时间和新的时间进行对比，不一致标示延期成功
     *
     * @param repaymentOrder
     * @param oldRepayDate
     */
    @Async("threadPoolTaskExecutor")
    @Override
    public void smsSendDeferd(RepaymentOrder repaymentOrder, String oldRepayDate) {
        log.info("发送延期短信：" + repaymentOrder.getLoanSerialNo() + "oldRepayDate,{}", oldRepayDate);
        LoanIou loanIou = loanIouService.getLoanIouByLoanSerialNo(repaymentOrder.getLoanSerialNo());
        String nowRepayDate = loanIou.getRepayDate();
        MonnifyReservedAccount monnifyReservedAccount = custVaService.getVaReservedAccount(loanIou.getCustNo());
        if (null != loanIou.getIsDeferred() && loanIou.getIsDeferred() == 1) {
            nowRepayDate = loanIou.getDeferredRepayDate();
            log.info("发送延期短信成功：" + repaymentOrder.getLoanSerialNo() + "oldRepayDate,{},nowRepayDate:{}", oldRepayDate, nowRepayDate);
            if (!oldRepayDate.equals(nowRepayDate)) {
                smsService.sendDelayProductSms(loanIou, repaymentOrder, "successful", nowRepayDate, monnifyReservedAccount);
                return;
            }
        }
        log.info("发送延期短信失败：" + repaymentOrder.getLoanSerialNo() + "oldRepayDate,{},nowRepayDate:{}", oldRepayDate, nowRepayDate);
        smsService.sendDelayProductSms(loanIou, repaymentOrder, "disabled", nowRepayDate, monnifyReservedAccount);
    }


    /**
     * 余额代扣短信
     * 未逾期借据代扣成功短信：
     * 逾期借据代扣成功短信：
     * 代扣成功且后借据结清短信：
     * @param loanSerialNo
     */
    @Async("threadPoolTaskExecutor")
    @Override
    public void batchCardWithhold(LoanIou loanIou,String loanSerialNo,RepaymentOrder repaymentOrder) {
        log.info("余额代扣短信IouStatus:{}", loanIou.getIouStatus());
        RealNameAuth realNameAuth = new RealNameAuth();
        realNameAuth.setCustNo(loanIou.getCustNo());
        realNameAuth = realNameAuthService.selectRealNameAuthList(realNameAuth).get(0);
        String lastName = realNameAuth.getLastName();
        String smsType = "";
        JSONObject smsJson = new JSONObject();
        smsJson.put("lastName", lastName);
        BigDecimal collectAmt = repaymentOrder.getRepaymentAmt();
        ProductInfo productInfo = orderService.selectLoanOrderProduct(loanIou.getCustNo());
        String noticeType = BizConstants.SMS_NOTICE_TYPE_NOTICE;

        if (loanIou.getIouStatus() == LoanIou.LOAN_IOU_STATUS_SETTLE) {
            //结清
            LoanOrder loanOrder = new LoanOrder();
            loanOrder.setLoanSerialNo(loanIou.getLoanSerialNo());
            List<LoanOrder> loanOrders = loanOrderService.selectLoanOrderList(loanOrder);
            smsJson.put("contractNo", loanOrders.get(0).getContractNo());
            smsJson.put("collectAmt", collectAmt);
            smsType = BizConstants.SMS_TYPE_WITHHOLD_SETTLE;
        } else if (loanIou.getIouStatus() == LoanIou.LOAN_IOU_STATUS_NORMAL) {
//            //查找monnify账号
            // todo wqg 找易讨论
            MonnifyReservedAccount monnifyReservedAccount = custVaService.getVaReservedAccount(loanIou.getCustNo());
            if (productInfo != null && loanIou.getIsDeferred() == 0) {
                //发送延期短信
                smsJson.put("credit_rating", sysConfigService.selectConfigKey(productInfo.getCreditRating()));
                smsJson.put("repaymentDate", loanIou.getRepayDate());
                smsType = BizConstants.SMS_TYPE_COLL_RPYMT_NTCE_DFR_T_0;
                noticeType = BizConstants.SMS_NOTICE_TYPE_REPAYMENT;
            } else {
                if (collectAmt.doubleValue() > 0) {
                    smsJson.put("collectAmt", collectAmt);
                    smsType = BizConstants.SMS_TYPE_WITHHOLD_WAIT_SUCCESS;
                } else {
                    smsType = BizConstants.SMS_TYPE_WITHHOLD_WAIT_FAILED;
                }
            }
            BigDecimal repaymentAmt = LoanIou.calculationOverdueAmount(loanIou);
            smsJson.put("RemainingAmt", repaymentAmt.setScale(2, BigDecimal.ROUND_UP));

            smsJson.put("VAaccountnumber", monnifyReservedAccount.getAccountNumber());
            smsJson.put("BankName", monnifyReservedAccount.getBankName());
        } else if (loanIou.getIouStatus() == LoanIou.LOAN_IOU_STATUS_OVERDUE) {
            //逾期
            MonnifyReservedAccount monnifyReservedAccount = custVaService.getVaReservedAccount(loanIou.getCustNo());
            BigDecimal principal = loanIou.getLoanPrincipal().subtract(loanIou.getRepaidPrincipal());
            List<ProductDeferredInfo> productDeferredInfoList = deferredInfoMapper.getProductDerferedsForShow(productInfo.getId(), principal, loanIou.getPeriodNum(), productInfo.getDeferredMaxDays());
            //满足权益，未使用，在保留期
            if (productInfo != null && loanIou.getIsDeferred() == 0 && productDeferredInfoList.size() > 0 && DateUtils.apartDay(loanIou.getRepayDate()) <= productDeferredInfoList.get(0).getNumDeferredRetainDay()) {
                smsJson.put("overdueDay", DateUtils.apartDay(loanIou.getRepayDate()));
                smsType = BizConstants.SMS_TYPE_COLL_RPYMT_NTCE_DFR_T_M1;
                noticeType = BizConstants.SMS_NOTICE_TYPE_REPAYMENT;
            } else {
                if (collectAmt.doubleValue() > 0) {
                    smsJson.put("collectAmt", collectAmt);
                    smsType = BizConstants.SMS_TYPE_WITHHOLD_OVERDUE_SUCCESS;
                } else {
                    smsType = BizConstants.SMS_TYPE_WITHHOLD_OVERDUE_FAILED;
                }
            }
            BigDecimal repaymentAmt = LoanIou.calculationOverdueAmount(loanIou);
            smsJson.put("RemainingAmt", repaymentAmt.setScale(2, BigDecimal.ROUND_UP));

            smsJson.put("VAaccountnumber", monnifyReservedAccount.getAccountNumber());
            smsJson.put("BankName", monnifyReservedAccount.getBankName());
        }
        if (!(smsType.equals(""))) {
            SmsTask smsTask = new SmsTask();
            smsTask.setUserId(realNameAuth.getUserId());
            smsTask.setCustNo(loanIou.getCustNo());
            smsTask.setCountryCode("+234");
            smsTask.setPhone(realNameAuth.getPhoneNumber());
            smsTask.setNoticeType(noticeType);
            smsTask.setSmsParam(JSON.toJSONString(smsJson));
            smsTask.setSmsType(smsType);
            smsTaskService.insertSmsTask(smsTask);
        }
    }


    @Async("threadPoolTaskExecutor")
    @Override
    public void activeRepayment(RepaymentOrder repaymentOrder, LoanIou loanIou, String repayStatus) {
//        LoanIou loanIouSelect = new LoanIou();
//        loanIouSelect.setLoanSerialNo(repaymentOrder.getLoanSerialNo());
//        List<LoanIou> loanIous = iLoanIouService.selectLoanIouList(loanIouSelect);
//        LoanIou loanIou = loanIous.get(0);

        RealNameAuth realNameAuth = new RealNameAuth();
        realNameAuth.setCustNo(repaymentOrder.getCustNo());
        realNameAuth = realNameAuthService.selectRealNameAuthList(realNameAuth).get(0);

        AppUser appUser = new AppUser();
        appUser.setUserUuid(realNameAuth.getUserId());
        List<AppUser> appUsers = appUserService.selectAppUserList(appUser);

        String lastName = realNameAuth.getLastName();
        String smsType = "";
        JSONObject smsJson = new JSONObject();
        smsJson.put("lastName", lastName);
        log.info("订单状态{}", loanIou.getIouStatus());
        int i = 0;
        if (loanIou.getIouStatus() == LoanIou.LOAN_IOU_STATUS_SETTLE) {
            //结清
            LoanOrder loanOrder = new LoanOrder();
            loanOrder.setLoanSerialNo(loanIou.getLoanSerialNo());
            List<LoanOrder> loanOrders = loanOrderService.selectLoanOrderList(loanOrder);
            //还款成功短信
            i = smsService.sendRepaymentPassSms(loanOrders.get(0).getCustNo(), appUsers.get(0));
            if (i == 0) {
                smsJson.put("contractNo", loanOrders.get(0).getContractNo());
                smsJson.put("repaymentAmt", repaymentOrder.getRepaymentAmt().setScale(2, BigDecimal.ROUND_UP));
                smsType = BizConstants.SMS_TYPE_REPAYMENT_FINISH;
            }

        } else if (loanIou.getIouStatus() == LoanIou.LOAN_IOU_STATUS_NORMAL) {

            //正常
            smsType = BizConstants.SMS_TYPE_REPAYEMNT_OUTSTADING;

            BigDecimal needTotal = loanIou.getLoanPrincipal().add(loanIou.getLoanInterest()).add(loanIou.getServiceFee()).add(loanIou.getLateFee());
            BigDecimal repaidTotal = loanIou.getRepaidPrincipal().add(loanIou.getRepaidInterest()).add(loanIou.getRepaidServiceFee()).add(loanIou.getRepaidLateFee());
            BigDecimal repaymentAmt = needTotal.subtract(repaidTotal);
            smsJson.put("remainingAmt", repaymentAmt.setScale(2, BigDecimal.ROUND_UP));
            smsJson.put("repaymentAmt", repaymentOrder.getRepaymentAmt().setScale(2, BigDecimal.ROUND_UP));
            smsJson.put("repaymentDate", DateUtils.formatDate(DateUtils.parseDate(loanIou.getRepayDate()), "dd-MM-yyyy"));
            smsJson.put("repayStatus", repayStatus);
            //查找monnify账号
            MonnifyReservedAccount monnifyReservedAccount = custVaService.getVaReservedAccount(loanIou.getCustNo());

            smsJson.put("VAaccountnumber", monnifyReservedAccount.getAccountNumber());
            smsJson.put("BankName", monnifyReservedAccount.getBankName());
        } else if (loanIou.getIouStatus() == LoanIou.LOAN_IOU_STATUS_OVERDUE) {
            //逾期
            smsType = BizConstants.SMS_TYPE_REPAYMENT_OVERDUE;

            BigDecimal needTotal = loanIou.getLoanPrincipal().add(loanIou.getLoanInterest()).add(loanIou.getServiceFee()).add(loanIou.getLateFee());
            BigDecimal repaidTotal = loanIou.getRepaidPrincipal().add(loanIou.getRepaidInterest()).add(loanIou.getRepaidServiceFee()).add(loanIou.getRepaidLateFee());
            BigDecimal repaymentAmt = needTotal.subtract(repaidTotal);
            smsJson.put("repayStatus", repayStatus);
            smsJson.put("repaymentAmt", repaymentOrder.getRepaymentAmt().setScale(2, BigDecimal.ROUND_UP));
            smsJson.put("remainingAmt", repaymentAmt.setScale(2, BigDecimal.ROUND_UP));
            //查找monnify账号
            MonnifyReservedAccount monnifyReservedAccount = custVaService.getVaReservedAccount(loanIou.getCustNo());

            smsJson.put("VAaccountnumber", monnifyReservedAccount.getAccountNumber());
            smsJson.put("BankName", monnifyReservedAccount.getBankName());
        }

        if (i == 0) {
            SmsSendData smsData = SmsSendData.builder().noticeType(BizConstants.SMS_NOTICE_TYPE_NOTICE).smsType(smsType)
                    .countryCode("234").phoneNumber(appUsers.get(0).getPhone()).tmplateId("100015").build();

            smsData.setSmsParam(smsJson);

            smsService.sendSms(smsData);
        }
    }

    //是否部分还款成功
    public BigDecimal isSuccess(String bathNo, String custNo) {
        BigDecimal bigDecimal = BigDecimal.ZERO;
        List<RepaymentOrder> repaymentOrderList = repaymentMapper.getNowByType(BizConstants.LOAN_REPAYMENT_ORDER_TYPE_DATE_BATCH, bathNo, custNo);
        for (int i = 0; i < repaymentOrderList.size(); i++) {
            if (repaymentOrderList.get(i).getRepayStatus() == 1) {
                bigDecimal = bigDecimal.add(repaymentOrderList.get(i).getRepaymentAmt());
            }
        }
        return bigDecimal;
    }
}
